<template>
  <section class="wrapper">
    <myHeader :obj="myObj" @events="events"></myHeader>
    <main>
      <nav class="nav_0320">
        <span :class="{'active_0320': select}" @click="changeSelect(true, 1)">语音</span>
        <span :class="{'active_0320': !select}" @click="changeSelect(false, 2)">视频</span>
      </nav>
      <div class="content_0320">
        <div class="uploadW_0320">
          <input type="file" class="uploadInput" :accept="select?'audio/*':'video/*'" @change="changeAudio($event)">
          <div class="icon_0320" :class="{'icon_0320_2': !select}"></div>
          <div class="infoW_0320">
            <h2 class="title_0320">{{select? '上传音频':'上传视频'}}</h2>
            <p class="info_0320">{{select? '我们支持MP3, WMA，AIFF，AIF, WAV, FLAC, OGG, MP2, AAC，AMR等文件格式，文件≤5M，请尽量上传高音质音频':'我们支持AVI, MP4，WMV，ASF, ASX, RM, RMVB, DAT, MKV，FLV等文件格式，请尽量上传高清视频'}}</p>
          </div>
        </div>
        <div class="isTrue" style=""><span>{{isStart?'开启中':'关闭中'}}</span><mt-switch v-model="isStart"></mt-switch><span @click="onOff()" style="display:block; position:absolute; width: .52rem; height: .32rem; right: .15rem;"></span></div>
         <input type="text" v-model.trim="MessageDetail.Title"  maxlength="10" class="intoTitle_0320" placeholder="请输入标题（20字内）">
         <textarea class="textarea_0320" v-model.trim="MessageDetail.Describe"  maxlength="300" placeholder="详细说明（300字内）"></textarea>
         <p class="aboutP_0320" >适合哪类人群（1-6个）</p>
         <div class="labelsW_0320">
           <myLabel :key="key" v-for="(item, key) in labels" :closeIcon="true" @removetag="removeLabel(item.RelationshipTallyId, key)">{{item.Tally}}</myLabel>
           <span v-if="labels.length < 6" @click="addLabel()">添加标签+</span>
         </div>
      </div>
      <div class="intoMoney_0320">定价 <input type="number" v-model="MessageDetail.Money" placeholder="金额输入"></div>
      <myButton class="marginB_4" @sendDate="clickBtn()">提交</myButton>
      <!-- <div>音频</div>
          <audio style="width: 100%;" :src="MessageDetail.VoiceUrl" controls="controls">
          </audio>
          <div>视频</div>
          <video style="width: 100%;" :src="MessageDetail.VideoUrl" controls="controls">
          your browser does not support the video tag
          </video> -->
    </main>
    <div id="upProgress" ref="upProgress">
      <div class="pro-in">
        <mt-spinner type="fading-circle" :size="80" color="#454545"></mt-spinner>
        <span class="clarify">上传中... <i id="proPercent">0%</i></span>
      </div>
    </div>
  </section>
</template>

<script>
import myHeader from './base/myHeader';
import myButton from './base/myButton';
import myLabel from './base/myLabel';
import { axiosPost} from '../assets/api';
import { MessageBox, Toast, Switch } from "mint-ui";
import Client from '../assets/js/common';
import Apis from '../assets/js/expertApi';
import Chat from '../assets/js/chat';
import hgjSkip from '../assets/js/hgjSkip';
export default {
  data () {
    return {
      login: null,
      myObj: { // 头部
        text: '',
        type: 2
      },
      isStart: false,
      select: true,
      PMId: null,
      labels: [],
      DelTallyArr: [],
      MessageDetail: { // 新增约见信息
        PostMessageId: this.$route.query.PostMessageId, // 行家信息主表id
        EKind: 1, // 添加的种类 0约见信息 1，知识信息
        Type: 1, // 类型 0.图文（约见必须为图文），1.语音 2.视频
        Title: "", // 标题
        Describe: "", // 描述
        PicUrl: "", // 背景图片链接
        ThumPicUrl: "", // 缩略图链接
        Money: 0, // 约见金额 约见信息时必须大于0
        Tallys: '', // 标签信息，最多6个，多个用,隔开
        VoiceUrl: '', // 语音链接 根据类型 判断可传必传
        VideoUrl: '', // 视频链接 根据类型 判断可传必传
        State: 0,
      }
    }
  },
  created () {
    hgjSkip.skip().then(res => {
      this.login = res;
    })
    this.PMId = this.$route.query.PostMessageId;
    if(this.PMId) {
      this.myObj.text = "内容发布";
    }else{
      this.myObj.text = "内容修改";
      this.myObj.type = 4;
      this.initUpdateDetails();
    }
  },
  methods: {
    events(i) {
      if(i ==2) {
        history.back();
      }else if ( i == 3) {
        this.delete();
      }
    },
    onOff() {
      if(this.MessageDetail.State) {
        this.MessageDetail.State = 0;
      }else{
        this.MessageDetail.State = 1;
      }
    },
    changeAudio(e) {
      let fileType = '';
      if(this.MessageDetail.Type == 1) {
        fileType = 'audio';
      }else if(this.MessageDetail.Type == 2) {
        fileType = 'video';
      }
      //控制上传进度
        let upProgress = this.$refs.upProgress;
        upProgress.style.display = 'block';
        document.querySelector('#proPercent').innerHTML = '0%';
        Chat.changePicFile(e, fileType).then(Res => {
          Toast('资源上传成功');
          if(this.MessageDetail.Type == 1) {
            this.MessageDetail.VoiceUrl = Res;
          }else if(this.MessageDetail.Type == 2) {
            this.MessageDetail.VideoUrl = Res;
          }
          upProgress.style.display = 'none';
        }).catch(err=>{
          upProgress.style.display = 'none';
          Toast('上传失败');
        });

    },
    removeLabel(isId, i) {
      if(isId){
        this.DelTallyArr.push(isId);
      }
      this.labels.splice(i, 1);
    },
    changeSelect(status, Type) {
      this.select = status;
      this.MessageDetail.Type = Type;
      
    },
    clickBtn() {
      if(this.MessageDetail.Type == 1) {
        this.MessageDetail.PicUrl = "http://ccnf-im-1255573444.cosgz.myqcloud.com/5478377965189740517.jpg"; // 背景图片链接
        this.MessageDetail.ThumPicUrl = 'http://ccnf-im-1255573444.cosgz.myqcloud.com/5478377965189740517.jpg';
      }else if(this.MessageDetail.Type == 2) {
        this.MessageDetail.PicUrl = "http://ccnf-im-1255573444.cosgz.myqcloud.com/5024669981490476977.jpg"; // 背景图片链接
        this.MessageDetail.ThumPicUrl = 'http://ccnf-im-1255573444.cosgz.myqcloud.com/5024669981490476977.jpg';
      }
      if(this.MessageDetail.Type == 1 && this.MessageDetail.VoiceUrl.length <= 0) return Toast("语音不能为空");
      if(this.MessageDetail.Type == 2 && this.MessageDetail.VideoUrl.length <= 0) return Toast("视频不能为空");
      if(this.MessageDetail.Title.length <= 0) return Toast("标题不能为空");
      if(!this.MessageDetail.Money || (parseFloat(this.MessageDetail.Money) <= 0)) return Toast("金额不能为空或小于零");
      if(parseFloat(this.MessageDetail.Money) >= 10000) return Toast("金额不能超过一万");
      this.addOrUpdateExpertDetail();
    },
    async addOrUpdateExpertDetail() {
      let addArrEle = []
      this.labels.forEach(e => {
        if(e.RelationshipTallyId == null) {
          addArrEle.push(e.Tally)
        }
      })
      if(this.PMId) { // 新增知识信息
        this.MessageDetail.Tallys = addArrEle.join(',');
        let result = await Apis.addExpertDetail({...this.login[1],MessageDetail: this.MessageDetail});
      }else { // 修改知识信息
        this.MessageDetail.AddTally = addArrEle.join(',');
        this.MessageDetail.DelTally = this.DelTallyArr.join(',');
        let result = await Apis.updateExpertDetail({...this.login[1],MessageDetail: this.MessageDetail});
      }
       history.back();
    },
    initUpdateDetails() {
      let currentObj = Client.getStore('MessageDetailItem');
      if(currentObj.Type == 1) {
        this.select = true;
      }else if(currentObj.Type == 2) {
        this.select = false;
      }
      this.MessageDetail.PostMessageId = currentObj.PostMessageId; // 行家信息主表id
      this.MessageDetail.PostMesssageDetailId = currentObj.EPostMesssageDetailId; // 行家信息明细表id
      this.MessageDetail.EKind = 1; // 添加的种类 0约见信息 1，知识信息
      this.MessageDetail.Type = currentObj.Type; // 类型 0.图文（约见必须为图文），1.语音 2.视频
      this.MessageDetail.Title = currentObj.Title; // 标题
      this.MessageDetail.Describe = currentObj.Describe; // 描述
      this.MessageDetail.Money = currentObj.Money; // 约见金额 约见信息时必须大于0
      this.MessageDetail.Tallys = currentObj.Tallys // 标签信息，最多6个，多个用,隔开
      this.labels = currentObj.Tallys // 标签信息，最多6个，多个用,隔开
      this.MessageDetail.VideoUrl = currentObj.VideoUrl // 视频链接 根据类型 判断可传必传
      this.MessageDetail.VoiceUrl = currentObj.VoiceUrl // 语音链接 根据类型 判断可传必传
      this.MessageDetail.State = currentObj.State;
    },
    delete() {
      Apis.deleteExpertDetail({...this.login[1],'PostMesssageDetailId': this.MessageDetail.PostMesssageDetailId})
      history.back();
    },
    addLabel() {
      let eleObj = {};
      eleObj.RelationshipTallyId = null;
       MessageBox.prompt("", {
        message: "添加标签",
        title: "",
        inputPlaceholder: "添加标签(只截取前5字)",
        closeOnClickModal: false // 点击遮罩层不关闭
      }).then(({value, action}) => {
        if(!value) return;
        let val = this.trim(value);
        let len = val.length;
        if(len<=0) return;
        if(len> 5){
          eleObj.Tally = val.slice(0,5);
          
        }else if(len > 0){
          eleObj.Tally = val;
        }
        this.labels.push(eleObj);
      },()=>{

      })
    },
    clickLabel(i) {
      this.label.splice(i,1);
    },
    trim(str){ 
      return str.replace(/(^\s*)|(\s*$)/g, ""); 
    }
  },
  watch: {
    "MessageDetail.State":function(){
      
      if(this.MessageDetail.State) {
        this.isStart = true;
      }else {
        this.isStart = false;
      }
    }
  },
  components: {
    myHeader,
    myButton,
    myLabel,
    mtSwitch: Switch
  }
}
</script>
<style lang="less" scoped>
.wrapper {
  width: 3.75rem;
  height: 100%;
  #upProgress{
      display: none;
      width:100%;
      height:100%;
      position: absolute;
      top:0;
      left:0;
      z-index:9999;
      background: rgba(255,255,255,.9);
      .pro-in{
        height:100%;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        display: flex;
        align-items: center;
      }
      .clarify{
        display: block;
        color:#454545;
        font-size: .14rem;
        padding-top:.16rem;
        i{
          font-size: .18rem;
          font-style: normal;
        }
      }
    }
  main {
    width: 100%;
    height: 100%;
    padding-top: .44rem;
    position: absolute;
    top: 0;
    left: 0;
    overflow-y: auto;
    box-sizing: border-box;
    .isTrue {
      height: .44rem;
      display: flex;
      justify-content:space-between;
      padding: 0 .15rem;
      box-sizing: border-box;
      background-color: white;
      align-items: center;
      position: relative; 
      border-top: .01rem solid #ccc;
      border-bottom: none;
      span {
        font-size: .13rem;
        color: #39393D;
      }
    }
    .marginB_4 {
      margin-bottom: .44rem;
    }
    .intoMoney_0320 {
      width: 100%;
      height: .44rem;
      background: white;
      display: flex;
      padding: 0 .15rem;
      box-sizing: border-box;
      align-items: center;
      justify-content: space-between;
      font-size: .17rem;
      color: #000000;
      line-height: .44rem;
      font-weight: 500;
      margin-bottom: .28rem;
      border-top: 1px solid #ddd;
      input {
        width: 2.7rem;
        height: .43rem;
        border: none;
        font-size: .17rem;
        color: #ADADAD;
      }
    }
    .content_0320 {
      padding: .36rem .15rem 0;
      background: white;
      .labelsW_0320 {
        padding-top: .04rem;
        overflow: hidden;
        span {
          display: block;
          height: .3rem;
          line-height: .3rem;
          float: left;
          margin: 0 .15rem .1rem 0;
          padding: 0 .1rem;
          background: #828282;
          font-size: .14rem;
          color: white;
          border-radius: .04rem;
        }
      }
      .aboutP_0320 {
        color: #353535;
        font-size: .14rem;
        padding-bottom: .08rem;
        
      }
      .textarea_0320 {
        width: 3.43rem;
        height: 2.18rem;
        font-size: .14rem;
        color: #ADADAD;
        border: .01rem solid #D8D8D8;
        border-radius: .03rem;
        padding: .12rem .15rem;
        box-sizing: border-box;
        margin-bottom: .21rem;
        appearance: none;
        -webkit-appearance: none;

      }
      .intoTitle_0320 {
        width: 3.43rem;
        height: .3rem;
        color: #ADADAD;
        font-size: .14rem;
        border: .01rem solid #D8D8D8;
        border-radius: .03rem;
        padding: 0 .15rem;
        box-sizing: border-box;
        margin-bottom: .15rem;
        line-height: .25rem;
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
      }
      .uploadW_0320 {
        width: 3.45rem;
        display: flex;
        justify-content: space-between;
        padding-bottom: .22rem;
        position: relative;
        .uploadInput {
          position: absolute;
          width: 100%;
          height: 100%;
          opacity: 0;
        }
        .icon_0320 {
          width: .73rem;
          height: .73rem;
          background: url('../assets/img/expert/icon_language-max@3x.png') center center;
          background-size: cover;
          background-repeat: no-repeat;
        }
        .icon_0320_2 {
          background: url('../assets/img/expert/icon_vido@3x.png') center center;
          background-size: cover;
          background-repeat: no-repeat;
        }
        .infoW_0320 {
          width: 2.46rem;
          .title_0320 {
            color: #39393D;
            font-size: .14rem;
            font-weight: 500;
            padding-bottom: .08rem;
          }
          .info_0320 {
            font-size: .12rem;
            line-height: .16rem;
            color: #888888;
          }
        }

      }

    }
    .nav_0320 {
      width: 100%;
      height: .45rem;
      padding: 0 .73rem;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      background: white;
      margin: .1rem auto .09rem;
      span {
        display: block;
        width: .7rem;
        height: .42rem;
        line-height: .42rem;
        text-align: center;
        font-size: .14rem;
        color: #828282;
        border-bottom: .04rem solid white;
        &.active_0320 {
          color: #557AFF;
          border-bottom-color: #557AFF;
        }
      }
    }
  }
}
</style>
<style>
  .mint-msgbox .mint-msgbox-input .mint-msgbox-errormsg {
    display: none;
  }
  .mint-msgbox-content .mint-msgbox-message {
    color: #000;
    font-weight: bold;
  }
  .mint-msgbox .mint-msgbox-input input {
    border: .01rem solid #ccc;
    box-sizing: border-box;
    height: .35rem;
    line-height: .35rem;
  }
</style>
